﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Mgrs;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.DL.ComponentScripts {
	//挂在空的父物体上
	public class DamageNumber : MonoBehaviour {
		[SerializeField] Animator animator;
		[SerializeField] float speed;
		float curSpeed;
		Vector2 dir;
		[SerializeField] Text text;
		public void Init(Vector2 dir, Vector3 pos, int dmg) {
			this.dir = dir;
			transform.position = pos;
			SetTextAhpla(1);
			curSpeed = speed;
			StartCoroutine(Fade());
			text.text = dmg.ToString();
		}

		public void Update() {
			transform.position += curSpeed * Time.deltaTime * (Vector3)dir;
		}


		IEnumerator Fade() {
			while (text.color.a > 0.01) {
				curSpeed = Mathf.Lerp(curSpeed, 0, 0.01f);
				yield return 0.1f;
			}
			ObjectPool.Instance.RecycleObject(ObjPoolStr.DmgNumber, gameObject);
		}

		void SetTextAhpla(float alpha) {
			var tmp = text.color;
			tmp.a = alpha;
			text.color = tmp;
		}
	}
}
